package com.ge.advanced;

import com.ge.advanced.pojo.User;

import java.sql.*;

public class JDBCAdvanced {

    public static void main(String[] args) throws Exception {

        // 数据库连接
        String url = "jdbc:mysql://localhost:3306/jdbc";
        String username = "root";
        String password = "123456";
        Connection connection = DriverManager.getConnection(url, username, password);

        String sql = "SELECT id, name, passwd FROM USER WHERE id = 1";
        PreparedStatement statement = connection.prepareStatement(sql);
        ResultSet resultSet = statement.executeQuery();

        User user = null;

        // 将查询结果封装为对象
        if (resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            String passed = resultSet.getString("passwd");
            user = new User(id, name, passed);
        }
        System.out.println(user);

        resultSet.close();
        statement.close();
        connection.close();
    }
}
